package cn.huijielong.common.utils;

import java.util.Objects;

import cn.huijielong.common.wxpay.sdk.WXPayConstants;

/**
 * 日志消息封装
 * @author xys
 *
 */
public final class LogMessage{
	private StringBuilder logMsg;
	private LogMessage(String className,String methodName) {
		logMsg=new StringBuilder(className).append("-").append(methodName);
	}
	/**
	 * @param className 类名称
	 * @param methodName 函数名称
	 * @return
	 */
	public static LogMessage create(String className,String methodName) {
		return new LogMessage(className,methodName);
	}
	/**
	 * 从Query 中获取日志数据
	 * @param q
	 * @return
	 */
	public String getMsg(Query q) {
		logMsg.append("{");
		String out_trade_no=q.getString("out_trade_no");
		String openid=q.getString("openid");
		String total_fee=q.getString("total_fee");
		
		String payType=q.getString("payType");
		String orderType=q.getString("orderType");
		String orderNo=q.getString("orderNo");
		
		Long userId=q.getLong("userId");
		Object payAmount=q.get("payAmount");
		
		if(out_trade_no.length()>0) {
			logMsg.append("out_trade_no=").append(out_trade_no).append(",");
		}
		if(openid.length()>0) {
			logMsg.append("openid=").append(openid).append(",");
		}
		if(total_fee.length()>0) {
			logMsg.append("total_fee=").append(total_fee).append(",");
		}
		
		if(userId.longValue()>0) {
			logMsg.append("userId=").append(userId).append(",");
		}
		if(payType.length()>0) {
			logMsg.append("payType=").append(payType).append(",");
		}
		if(orderType.length()>0) {
			logMsg.append("orderType=").append(orderType).append(",");
		}
		
		if(orderNo.length()>0) {
			logMsg.append("orderNo=").append(orderNo).append(",");
		}
		if(!Objects.isNull(payAmount)) {
			logMsg.append("payAmount=").append(payAmount).append(",");
		}
	
		logMsg.append("}");
		return logMsg.toString();
	}
	
	public StringBuilder getMsg() {
		return logMsg;
	}
	
	public LogMessage append(String str) {
		logMsg.append(str);
		return this;
	}
	/**
	 * 对append()进入的数据格式化  %d %f %.2f(2位小数) %s
	 * @param arguments
	 * @return
	 */
	public String format(Object...arguments) {
		return String.format(logMsg.toString(), arguments);
	}
}
